#!/usr/bin/env python
# -*-coding:utf-8-*-

import os
import docx
import docxtpl
import xlrd
from docx.shared import Inches

data = xlrd.open_workbook(os.path.abspath("现状规划.xls"))
sheet = data.sheets()
table0 = sheet[0]


def creat_result_dir():
    a = input('命名成果文件夹：')
    try:
        os.mkdir(a)
    except(FileExistsError):
        print('文件夹已经存在')
    except:
        print('未知异常')
    return a



def docCreate():
    a = creat_result_dir()
    for row in range(1, table0.nrows):
        try:
            t = table0.row_values(row)
            tpl = docxtpl.DocxTemplate(os.path.abspath('模板.docx'))
            image1_path = os.path.join(os.path.abspath('规划'), '%s.jpg' % str(t[1]))
            image2_path = os.path.join(os.path.abspath('现状'), '%s.jpg' % str(t[1]))
            path_images3 = os.path.join(os.path.abspath('现场'), '%s' % str(t[1]))
            images = os.listdir(os.path.abspath(path_images3))
            if len(images) > 1:
                print('2')
                images3 = os.path.join(path_images3, images[0])
                images4 = os.path.join(path_images3, images[1])
                insert_image3 = docxtpl.InlineImage(tpl, images3, width=Inches(5))
                insert_image4 = docxtpl.InlineImage(tpl, images4, width=Inches(5))
                insert_image1 = docxtpl.InlineImage(tpl, image1_path, width=Inches(5))
                insert_image2 = docxtpl.InlineImage(tpl, image2_path, width=Inches(5))
    
                # insert_image5 = docxtpl.InlineImage(tpl, images5, width=Inches(5))
                context = {
                    'template0': t[0],
                    'template1': t[1],
                    'template2': t[2],
                    'image1': insert_image1,
                    'image2': insert_image2,
                    'image3': insert_image3,
                    'image4': insert_image4,
                }
                tpl.render(context)
                result = os.path.join(os.path.abspath(a), '%s.docx' % t[1])
                tpl.save(result)
                doc2 = docx.Document(result)
                print('完成 %s' % str(t[1]))
                doc2.save(result)
            else:
    
                images3 = os.path.join(path_images3, images[0])
                insert_image3 = docxtpl.InlineImage(tpl, images3, width=Inches(5))
                insert_image1 = docxtpl.InlineImage(tpl, image1_path, width=Inches(5))
                insert_image2 = docxtpl.InlineImage(tpl, image2_path, width=Inches(5))
    
                # insert_image5 = docxtpl.InlineImage(tpl, images5, width=Inches(5))
                context = {
                    'template0': t[0],
                    'template1': t[1],
                    'template2': t[2],
                    'image1': insert_image1,
                    'image2': insert_image2,
                    'image3': insert_image3,
                }
                tpl.render(context)
                result = os.path.join(os.path.abspath(a), '%s.docx' % t[1])
                tpl.save(result)
                doc2 = docx.Document(result)
                print('完成 %s' % str(t[1]))
                doc2.save(result)
    
        except:
            print(EOFError)

docCreate()
